﻿var language;

function ShowLoadingIndicator() {
    if (typeof(disableLoadingIndicator) != 'undefined' && disableLoadingIndicator) {
	    return;
    }
    var windowWidth = $j(window).width();
    var scrollTop;
    if(self.pageYOffset) {
	    scrollTop = self.pageYOffset;
    }
    else if(document.documentElement && document.documentElement.scrollTop) {
	    scrollTop = document.documentElement.scrollTop;
    }
    else if(document.body) {
	    scrollTop = document.body.scrollTop;
    }
    $j('#AjaxLoading').css('position', 'absolute');
    $j('#AjaxLoading').css('top', scrollTop + 'px');
    $j('#AjaxLoading').css('left', parseInt((windowWidth-150)/2)+"px");
    $j('#AjaxLoading').show();
}
function HideLoadingIndicator() {
    $j('#AjaxLoading').delay(800).slideUp(300);
}

$j(document).ready(function () {
    if (language == "vi-VN")
        $j("#AjaxLoading").html("<img src=\"/images/ajax-load.gif\" />&nbsp; Đang tải... Vui lòng chờ...");
    else
        $j("#AjaxLoading").html("<img src=\"/images/ajax-load.gif\" />&nbsp; Loading... Please wait...");

    $j('html').ajaxStart(function () {
        ShowLoadingIndicator();
    });
    $j('html').ajaxComplete(function () {
        HideLoadingIndicator();
    });
    $j('.InitialFocus').focus();
});

function PopupCartAmount(id, amount) {
    var optionVars = "0";
    if ($j(".CartOptionId").length > 0) { optionVars = $j(".CartOptionId").val(); }
    var querystring = "{'productId':'" + id + "', 'productAmount':'" + amount + "', 'optionVars':'" + optionVars + "'}"
    $j.ajax({
        beforeSend: function () { },
        type: "POST",
        url: "/WebServices/ProductService.asmx/ViewProductCart2",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: querystring,
        success: function (response) {
            var p = response.d;
            $j('#inline1').html(p.ValueReturn);
            console.log("Done!");
        },
        failure: function (msg) { console.log(msg); },
        error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status + ': ' + thrownError); },
        complete: function (msg) { ReloadCart(); },
        afterClose: function () { $j('#inline1').html(""); }
    });
    return false;
}

function PopupCart(id) {
    var displayPopup = true;
    var popupcontent = $j("#inline1").html().trim();
    if (popupcontent != "") {
        var productId = Number($j(".popup-cart").find(".fastCartTop").attr("productid"));
        if (productId == id) { displayPopup = false; }
    }
    if (displayPopup == true) {
        $j.ajax({
            beforeSend: function () { },
            type: "POST",
            url: "/WebServices/ProductService.asmx/ViewProductCart",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'id':'" + id + "'}",
            success: function (response) { $j('#inline1').html(response.d.ValueReturn); },
            failure: function (msg) { console.log(msg); },
            error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status + ': ' + thrownError); },
            complete: function (msg) { ReloadCart(); }
        });
        $j("a.ProductAddButton").trigger("click");
    }
    else {
        $j.ajax({
            beforeSend: function () { },
            type: "POST",
            url: "/WebServices/ProductService.asmx/UpdateProductAmount",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'productId':'" + id + "'}",
            success: function (response) {},
            failure: function (msg) { console.log(msg); },
            error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status + ': ' + thrownError); },
            complete: function (msg) { ReloadCart(); }
        });

        var quantity = Number($j(".quantity").find("span").html());
        quantity = quantity + 1;
        $j(".quantity").find("span").html(quantity);

        var sum = Number($j("#fastCartNumItemsTxt").html());
        sum = sum + 1;
        $j("#fastCartNumItemsTxt").html(sum);
    }
    return false;
}
function ReloadCart() {
    $j.ajax({
        type: "POST",
        url: "/WebServices/ProductService.asmx/LoadCart",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var p = response.d;
            $j('#plist').fadeTo("slow", 0.8);
            $j('#plist').html(p.ValueReturn);
            $j('#plist').fadeTo('slow', 1);
        },
        failure: function (msg) { console.log(msg); },
        error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status + ': ' + thrownError); },
        complete: function (msg) {
            if ($j("#pInCart").html() != null) 
                $j("#pInTop").html(' (' + $j("#pInCart").html() + ')');
            else $j("#pInTop").html('');
            $j(".BulkDiscount").show();
        }
    });
}
function RemoveProductCart(productId, optionVars) {
    $j.ajax({
        beforeSend: function () {},
        type: "POST",
        url: "/WebServices/ProductService.asmx/RemoveProductInCart",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'productId':'" + productId + "', 'optionVars':'" + optionVars + "'}",
        success: function (response) {
            var p = response.d;
            $j('#plist').fadeTo("slow", 0.8);
            $j('#plist').html(p.ValueReturn);
            $j('#plist').fadeTo("slow", 1);
        },
        failure: function (msg) { console.log(msg); },
        error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status + ': ' + thrownError); },
        complete: function (msg) {
            if ($j("#pInCart").html() != null) $j("#pInTop").html(' (' + $j("#pInCart").html() + ')');
            else $j("#pInTop").html('');
        }
    });
    return false;
}

function PopupCartDetail() {
    var inventory = $j('.VariationProductInventory').html();
    var amount = $j("[id$='qty_']").val();
    if (Number(amount) > Number(inventory)) {
        alert('Số lượng đặt mua không được lớn hơn số lượng còn trong kho !');
    }
    else {
        PopupCartAmount(ProductId, amount);
        $j(".BulkDiscount").hide();
        $j("a.ProductAddButton").trigger("click");
    }
}

$j(function () {
    $j("a.ProductAddButton").fancybox({
        width: 800, height: 550, fitToView: false, autoSize: false, openEffect: "fade", closeEffect: "fade",
        afterClose: function () {
            $j("#inline1").html("");
        }
    });
});

function check_add_to_cart(form, required) {
    // validate the attributes
    var attributesValidated = $j('form')
		.validate().form();

    if (!attributesValidated) {
        return false;
    }

    //popup cart
    PopupCartDetail();
    return false;
}

$j(document).ready(function () {
    var menus = $j("#nav ul.level0");
    menus.each(function (index, menu) {
        var childs = $j(menu).children().length;
        if (childs == 0) $j(menu).remove()
    });
    menus = $j("#nav ul.level1"); menus.each(function (index, menu) {
        var childs = $j(menu).children().length;
        if (childs == 0) $j(menu).remove()
    })
});
function toggleMenu(el1, el2, over) {
    var p; var prefix = "nav-";
    if (el2 == -1) { p = document.getElementById(prefix + el1) }
    else { p = document.getElementById(prefix + el1 + "-" + el2) }
    var newClassName;
    if (over == 1) { 
        newClassName = p.className + " over"; p.className = newClassName }
    else {
        newClassName = p.className.replace(" over", ""); p.className = newClassName
    } 
}